home *** CD-ROM | disk | FTP | other *** search
Text File | 1987-07-08 | 3.4 KB | 107 lines | [TEXT/MACA] |
- {
- I found the following in Borland's programming SIG. It shows how to
- implement procedural parameters in Turbo and TML. I have mixed feelings
- about procedural parameters. They are included in the ISO standard and
- in MPW Pascal. On the other hand, they're tricksy boogers and I suspect
- that most programs would be better if they were written without them.
- Like the GoTo, they are anacronisms. Nuff said. If you need em, here
- they are.
- }
-
- PROGRAM Example_of_Procedure_Parameters(input,output);
-
- { Shows how to use the pass procedures as parameters
-
- Author: Mike Babulic (Compuserve ID 72307,314)
- 3827 Charleswood Dr. N.W.,
- Calgary, Alberta
- CANADA, T2L 2C7
-
- One of the weaknesses of TURBO Pascal is that procedures and functions
- cannot be passed as parameters. Here is a method to overcome this limitation.
-
- Procedures CAN be passed as parameters to other procedures by creative use
- of the INLINE statement!
-
- It is accomplished by defining a dummy procedure (or function) with exactly
- the same arguments as the procedure to be passed EXCEPT that a final added
- parameter of type ProcPtr is also defined. When the calling procedure calls
- the dummy procedure, it passes the address of the procedure to be executed in
- this last parameter.
-
- NOTE: The procedure parameter MUST BE A GLOBAL PROCEDURE or strange & wonderful
- things may happen to your program!!!!
- }
-
- {TML Pascal
- USES MacIntf;
- }
- {TURBO Pascal}
-
- USES MemTypes;
-
-
- {----------------- Procedures to be passed as parameters -----------------}
- {n.b.Procedures MUST be GLOBAL}
-
- PROCEDURE theFirst(a,b:integer);
- begin
- WriteLn(' FIRST ',a:3,b:3);
- end;
-
- PROCEDURE theSecond(a,b:integer);
- begin
- WriteLn(a:3,' SECOND',b:3);
- end;
-
- PROCEDURE theThird(a,b:integer);
- begin
- WriteLn(a:3,b:3,' THIRD');
- end;
-
- {------------------ Routine with procedure parameter ------------------}
-
- PROCEDURE OneTwo(p:ProcPtr); { <-- procedure p is passed as a parameter}
- { n.b. p MUST be a GLOBAL procedure}
- PROCEDURE dummy(a,b:integer;p:ProcPtr); { <-- dummy procedure call}
- INLINE
- $205F, {MOVE.L (SP)+,A0 }
- $4E90; {JSR (A0) }
- begin
- dummy(9,10,p);
- end;
-
- {---------------- Procedure Pointers Stored in an Array --------------}
-
- VAR ProcArray : ARRAY [1..3] OF ProcPtr;{ <-- array of Procedure Pointers}
- { n.b. MUST be GLOBAL procedures}
- PROCEDURE dummy(a,b:integer;p:ProcPtr);{ <-- p is stored in the array!}
- INLINE
- $205F, {MOVE.L (SP)+,A0 }
- $4E90; {JSR (A0) }
-
- {------------------ Demonstrate Procedure Pointers -----------------}
-
- VAR
- i : integer;
- c:char;
-
- BEGIN
-
- {Using Procedure Parameters}
- OneTwo(@theFirst); {will Execute theFirst}
- OneTwo(@theSecond); {will Execute theSecond}
- OneTwo(@theThird); {will Execute theThird}
-
- WriteLn;
-
- {Using an array of Procedure Pointers}
- ProcArray[1] := @theFirst;
- ProcArray[2] := @theSecond;
- ProcArray[3] := @theThird;
- for i := 3 downto 1 do
- dummy(i,i+1,ProcArray[i]);
-
- WriteLn; WriteLn('Press RETURN to Quit');
- Read(c);
- END.